some more oracle-phpunit-fu (should not affect non-oracle)
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
1 <?php
2
3 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
4 public $suite;
5 public $regex = '';
6 public $runDisabled = false;
7
8 /**
9 * @var DatabaseBase
10 */
11 protected $db;
12 protected $oldTablePrefix;
13 protected $useTemporaryTables = true;
14 protected $reuseDB = false;
15 private static $dbSetup = false;
16
17 /**
18 * Table name prefixes. Oracle likes it shorter.
19 */
20 const DB_PREFIX = 'unittest_';
21 const ORA_DB_PREFIX = 'ut_';
22
23 protected $supportedDBs = array(
24 'mysql',
25 'sqlite',
26 'postgres',
27 'oracle'
28 );
29
30 function __construct( $name = null, array $data = array(), $dataName = '' ) {
31 parent::__construct( $name, $data, $dataName );
32
33 $this->backupGlobals = false;
34 $this->backupStaticAttributes = false;
35 }
36
37 function run( PHPUnit_Framework_TestResult $result = NULL ) {
38 /* Some functions require some kind of caching, and will end up using the db,
39 * which we can't allow, as that would open a new connection for mysql.
40 * Replace with a HashBag. They would not be going to persist anyway.
41 */
42 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
43
44 if( $this->needsDB() ) {
45 global $wgDBprefix;
46
47 $this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
48 $this->reuseDB = $this->getCliArg('reuse-db');
49
50 $this->db = wfGetDB( DB_MASTER );
51
52 $this->checkDbIsSupported();
53
54 $this->oldTablePrefix = $wgDBprefix;
55
56 if( !self::$dbSetup ) {
57 $this->initDB();
58 self::$dbSetup = true;
59 }
60
61 $this->addCoreDBData();
62 $this->addDBData();
63
64 parent::run( $result );
65
66 $this->resetDB();
67 } else {
68 parent::run( $result );
69 }
70 }
71
72 function dbPrefix() {
73 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
74 }
75
76 function needsDB() {
77 $rc = new ReflectionClass( $this );
78 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
79 }
80
81 /**
82 * Stub. If a test needs to add additional data to the database, it should
83 * implement this method and do so
84 */
85 function addDBData() {}
86
87 private function addCoreDBData() {
88 if ( $this->db->getType() == 'oracle' ) {
89
90 # Insert 0 user to prevent FK violations
91 # Anonymous user
92 $this->db->insert( 'user', array(
93 'user_id' => 0,
94 'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
95
96 # Insert 0 page to prevent FK violations
97 # Blank page
98 $this->db->insert( 'page', array(
99 'page_id' => 0,
100 'page_namespace' => 0,
101 'page_title' => ' ',
102 'page_restrictions' => NULL,
103 'page_counter' => 0,
104 'page_is_redirect' => 0,
105 'page_is_new' => 0,
106 'page_random' => 0,
107 'page_touched' => $this->db->timestamp(),
108 'page_latest' => 0,
109 'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
110
111 }
112
113 User::resetIdByNameCache();
114
115 //Make sysop user
116 $user = User::newFromName( 'UTSysop' );
117
118 if ( $user->idForName() == 0 ) {
119 $user->addToDatabase();
120 $user->setPassword( 'UTSysopPassword' );
121
122 $user->addGroup( 'sysop' );
123 $user->addGroup( 'bureaucrat' );
124 $user->saveSettings();
125 }
126
127
128 //Make 1 page with 1 revision
129 $article = new Article( Title::newFromText( 'UTPage' ) );
130 $article->doEdit( 'UTContent',
131 'UTPageSummary',
132 EDIT_NEW,
133 false,
134 User::newFromName( 'UTSysop' ) );
135 }
136
137 private function initDB() {
138 global $wgDBprefix;
139 if ( $wgDBprefix === $this->dbPrefix() ) {
140 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
141 }
142
143 $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() );
144 $dbClone->useTemporaryTables( $this->useTemporaryTables );
145
146 if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) {
147 CloneDatabase::changePrefix( $this->dbPrefix() );
148 $this->resetDB();
149 return;
150 } else {
151 $dbClone->cloneTableStructure();
152 }
153
154 if ( $this->db->getType() == 'oracle' ) {
155 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
156 }
157 }
158
159 /**
160 * Empty all tables so they can be repopulated for tests
161 */
162 private function resetDB() {
163 if( $this->db ) {
164 if ( $this->db->getType() == 'oracle' ) {
165 if ( $this->useTemporaryTables ) {
166 wfGetLB()->closeAll();
167 $this->db = wfGetDB( DB_MASTER );
168 } else {
169 foreach( $this->listTables() as $tbl ) {
170 if( $tbl == 'interwiki') continue;
171 $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
172 }
173 }
174 } else {
175 foreach( $this->listTables() as $tbl ) {
176 if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
177 $this->db->delete( $tbl, '*', __METHOD__ );
178 }
179 }
180 }
181 }
182
183 protected function destroyDB() {
184 if ( is_null( $this->db ) ||
185 ( $this->useTemporaryTables && $this->db->getType() != 'oracle' ) ||
186 ( $this->reuseDB ) ) {
187 # Don't need to do anything
188 return;
189 }
190
191 $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
192
193 foreach ( $tables as $table ) {
194 try {
195 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
196 $this->db->query( $sql, __METHOD__ );
197 } catch( MWException $mwe ) {}
198 }
199
200 if ( $this->db->getType() == 'oracle' )
201 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
202
203 CloneDatabase::changePrefix( $this->oldTablePrefix );
204 }
205
206
207 function __call( $func, $args ) {
208 static $compatibility = array(
209 'assertInternalType' => 'assertType',
210 'assertNotInternalType' => 'assertNotType',
211 'assertInstanceOf' => 'assertType',
212 'assertEmpty' => 'assertEmpty2',
213 );
214
215 if ( method_exists( $this->suite, $func ) ) {
216 return call_user_func_array( array( $this->suite, $func ), $args);
217 } elseif ( isset( $compatibility[$func] ) ) {
218 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
219 } else {
220 throw new MWException( "Called non-existant $func method on "
221 . get_class( $this ) );
222 }
223 }
224
225 private function assertEmpty2( $value, $msg ) {
226 return $this->assertTrue( $value == '', $msg );
227 }
228
229 static private function unprefixTable( $tableName ) {
230 global $wgDBprefix;
231 return substr( $tableName, strlen( $wgDBprefix ) );
232 }
233
234 static private function isNotUnittest( $table ) {
235 return strpos( $table, 'unittest_' ) !== 0;
236 }
237
238 protected function listTables() {
239 global $wgDBprefix;
240
241 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
242 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
243
244 // Don't duplicate test tables from the previous fataled run
245 $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
246
247 if ( $this->db->getType() == 'sqlite' ) {
248 $tables = array_flip( $tables );
249 // these are subtables of searchindex and don't need to be duped/dropped separately
250 unset( $tables['searchindex_content'] );
251 unset( $tables['searchindex_segdir'] );
252 unset( $tables['searchindex_segments'] );
253 $tables = array_flip( $tables );
254 }
255 return $tables;
256 }
257
258 protected function checkDbIsSupported() {
259 if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
260 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
261 }
262 }
263
264 public function getCliArg( $offset ) {
265
266 if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
267 return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
268 }
269
270 }
271
272 public function setCliArg( $offset, $value ) {
273
274 MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
275
276 }
277
278 public static function disableInterwikis( $prefix, &$data ) {
279 return false;
280 }
281
282 /**
283 * Don't throw a warning if $function is deprecated and called later
284 *
285 * @param $function String
286 * @return null
287 */
288 function hideDeprecated( $function ) {
289 wfSuppressWarnings();
290 wfDeprecated( $function );
291 wfRestoreWarnings();
292 }
293 }